package utils.sl651;

import java.util.ArrayList;
import java.util.List;

/**
 * SL651专业数据解析增强模块
 * 完善气象数据、设备状态、时间序列数据的专业解析逻辑
 * 给出当前数据的判断比如大风还是异常
 */
public class SL651DataParsingEnhancer {
    
    /**
     * 增强气象数据解析
     */
    public static void enhanceWeatherDataParsing(SL651Message message) {
        // 温度数据专业解析
        String temperature = message.getDataElement(SL651Protocol.ELEM_TEMPERATURE);
        if (temperature != null) {
            parseTemperatureData(temperature, message);
        }
        
        // 风向风速数据解析
        String windDirection = message.getDataElement(SL651Protocol.ELEM_WIND_DIRECTION);
        String windSpeed = message.getDataElement(SL651Protocol.ELEM_WIND_SPEED);
        if (windDirection != null && windSpeed != null) {
            parseWindData(windDirection, windSpeed, message);
        }
        
        // 气压数据解析
        String pressure = message.getDataElement(SL651Protocol.ELEM_PRESSURE);
        if (pressure != null) {
            parsePressureData(pressure, message);
        }
        
        // 可见度数据解析
        String visibility = message.getDataElement(SL651Protocol.ELEM_VISIBILITY);
        if (visibility != null) {
            parseVisibilityData(visibility, message);
        }
    }
    
    /**
     * 增强设备状态数据解析
     */
    public static void enhanceDeviceStatusParsing(SL651Message message) {
        // 电压状态分析
        String voltage = message.getDataElement(SL651Protocol.ELEM_VOLTAGE);
        if (voltage != null) {
            parseVoltageStatus(voltage, message);
        }
        
        // 通信状态分析
        analyzeCommunicationStatus(message);
        
        // 设备运行状态
        analyzeDeviceOperationStatus(message);
    }
    
    /**
     * 增强时间序列数据解析
     */
    public static void enhanceTimeSeriesDataParsing(SL651Message message) {
        // 5分钟间隔降水量序列
        String drpData = message.getDataElement(SL651Protocol.ELEM_5MIN_RAINFALL);
        if (drpData != null) {
            parseRainfallTimeSeries(drpData, message);
        }
        
        // 观测时间序列分析
        String observeTime = message.getDataElement(SL651Protocol.ELEM_OBSERVE_TIME);
        if (observeTime != null) {
            parseObservationTimeSequence(observeTime, message);
        }
    }
    
    /**
     * 增强基础数据元素解析
     */
    public static void enhanceBasicDataElements(SL651Message message) {
        // 对已解析的数据元素进行单位转换和格式化
        
        // 处理降水量数据
        String rainfall = message.getDataElement(SL651Protocol.ELEM_CURRENT_RAINFALL);
        if (rainfall != null) {
            try {
                double value = Double.parseDouble(rainfall);
                message.addDataElement("RAINFALL_FORMATTED", String.format("%.1f mm", value));
                
                // 降水等级判断
                String level = getRainfallLevel(value);
                message.addDataElement("RAINFALL_LEVEL", level);
            } catch (NumberFormatException e) {
                message.addDataElement("RAINFALL_FORMAT_ERROR", "降水量数据格式错误");
            }
        }
        
        // 处理水位数据
        String waterLevel = message.getDataElement(SL651Protocol.ELEM_WATER_LEVEL);
        if (waterLevel != null) {
            try {
                double value = Double.parseDouble(waterLevel);
                message.addDataElement("WATER_LEVEL_FORMATTED", String.format("%.3f m", value));
                
                // 水位警戒等级（需要配置警戒值）
                String alert = getWaterLevelAlert(value);
                message.addDataElement("WATER_LEVEL_ALERT", alert);
            } catch (NumberFormatException e) {
                message.addDataElement("WATER_LEVEL_FORMAT_ERROR", "水位数据格式错误");
            }
        }
        
        // 处理电压数据
        String voltage = message.getDataElement(SL651Protocol.ELEM_VOLTAGE);
        if (voltage != null) {
            try {
                double value = Double.parseDouble(voltage);
                message.addDataElement("VOLTAGE_FORMATTED", String.format("%.2f V", value));
                
                // 电压状态判断
                String status = getVoltageStatus(value);
                message.addDataElement("VOLTAGE_STATUS", status);
            } catch (NumberFormatException e) {
                message.addDataElement("VOLTAGE_FORMAT_ERROR", "电压数据格式错误");
            }
        }
        
        // 处理流量数据
        String flowRate = message.getDataElement(SL651Protocol.ELEM_FLOW_RATE);
        if (flowRate != null) {
            try {
                double value = Double.parseDouble(flowRate);
                message.addDataElement("FLOW_RATE_FORMATTED", String.format("%.3f m³/s", value));
                
                // 流量等级判定
                String level = getFlowRateLevel(value);
                message.addDataElement("FLOW_RATE_LEVEL", level);
            } catch (NumberFormatException e) {
                message.addDataElement("FLOW_RATE_FORMAT_ERROR", "流量数据格式错误");
            }
        }
        
        // 处理水温数据
        String waterTemp = message.getDataElement(SL651Protocol.ELEM_WATER_TEMP);
        if (waterTemp != null) {
            try {
                double value = Double.parseDouble(waterTemp);
                message.addDataElement("WATER_TEMP_FORMATTED", String.format("%.1f °C", value));
                
                // 水温状态判定
                String status = getWaterTempStatus(value);
                message.addDataElement("WATER_TEMP_STATUS", status);
            } catch (NumberFormatException e) {
                message.addDataElement("WATER_TEMP_FORMAT_ERROR", "水温数据格式错误");
            }
        }
        
        // 处理PH值数据
        String pH = message.getDataElement(SL651Protocol.ELEM_PH);
        if (pH != null) {
            try {
                double value = Double.parseDouble(pH);
                message.addDataElement("PH_FORMATTED", String.format("%.2f", value));
                
                // PH值等级判定
                String level = getPhLevel(value);
                message.addDataElement("PH_LEVEL", level);
            } catch (NumberFormatException e) {
                message.addDataElement("PH_FORMAT_ERROR", "PH值数据格式错误");
            }
        }
        
        // 处理溶解氧数据
        String dissolvedOxygen = message.getDataElement(SL651Protocol.ELEM_DISSOLVED_OXYGEN);
        if (dissolvedOxygen != null) {
            try {
                double value = Double.parseDouble(dissolvedOxygen);
                message.addDataElement("DO_FORMATTED", String.format("%.2f mg/L", value));
                
                // 溶解氧水质评级
                String quality = getDissolvedOxygenQuality(value);
                message.addDataElement("DO_QUALITY", quality);
            } catch (NumberFormatException e) {
                message.addDataElement("DO_FORMAT_ERROR", "溶解氧数据格式错误");
            }
        }
        
        // 处理电导率数据
        String conductivity = message.getDataElement(SL651Protocol.ELEM_CONDUCTIVITY);
        if (conductivity != null) {
            try {
                double value = Double.parseDouble(conductivity);
                message.addDataElement("COND_FORMATTED", String.format("%.0f μS/cm", value));
                
                // 电导率水质分级
                String grade = getConductivityGrade(value);
                message.addDataElement("COND_GRADE", grade);
            } catch (NumberFormatException e) {
                message.addDataElement("COND_FORMAT_ERROR", "电导率数据格式错误");
            }
        }
        
        // 处理浊度数据
        String turbidity = message.getDataElement(SL651Protocol.ELEM_TURBIDITY);
        if (turbidity != null) {
            try {
                double value = Double.parseDouble(turbidity);
                message.addDataElement("TU_FORMATTED", String.format("%.1f NTU", value));
                
                // 浊度水质等级
                String quality = getTurbidityQuality(value);
                message.addDataElement("TU_QUALITY", quality);
            } catch (NumberFormatException e) {
                message.addDataElement("TU_FORMAT_ERROR", "浊度数据格式错误");
            }
        }
    }
    
    /**
     * 分析报警条件
     */
    public static void analyzeAlarmConditions(SL651Message message) {
        List<String> alarmReasons = new ArrayList<>();
        
        // 检查降水量是否超标
        String rainfall = message.getDataElement(SL651Protocol.ELEM_CURRENT_RAINFALL);
        if (rainfall != null) {
            try {
                double value = Double.parseDouble(rainfall);
                if (value > 50.0) { // 假设50mm为警戒值
                    alarmReasons.add("降水量超过警戒值(" + value + "mm)");
                }
            } catch (NumberFormatException ignored) {}
        }
        
        // 检查水位是否超标
        String waterLevel = message.getDataElement(SL651Protocol.ELEM_WATER_LEVEL);
        if (waterLevel != null) {
            try {
                double value = Double.parseDouble(waterLevel);
                if (value > 10.0) { // 假设10m为警戒值
                    alarmReasons.add("水位超过警戒值(" + value + "m)");
                }
            } catch (NumberFormatException ignored) {}
        }
        
        // 检查电压是否异常
        String voltage = message.getDataElement(SL651Protocol.ELEM_VOLTAGE);
        if (voltage != null) {
            try {
                double value = Double.parseDouble(voltage);
                if (value < 10.0 || value > 15.0) { // 假设正常范围10-15V
                    alarmReasons.add("电压异常(" + value + "V)");
                }
            } catch (NumberFormatException ignored) {}
        }
        
        if (!alarmReasons.isEmpty()) {
            message.addDataElement("ALARM_REASONS", String.join("; ", alarmReasons));
        } else {
            message.addDataElement("ALARM_REASONS", "未检测到明确的报警原因");
        }
    }
    
    /**
     * 解析时间序列数据
     */
    public static void parseTimeSeriesData(SL651Message message) {
        // 解析DRP时间序列数据
        String drpParsed = message.getDataElement("DRP_PARSED");
        if (drpParsed != null) {
            String[] values = drpParsed.split(",");
            double totalRainfall = 0;
            int validCount = 0;
            
            for (String value : values) {
                if (!value.contains("无效") && value.contains("mm")) {
                    try {
                        double rain = Double.parseDouble(value.replace("mm", ""));
                        totalRainfall += rain;
                        validCount++;
                    } catch (NumberFormatException ignored) {}
                }
            }
            
            if (validCount > 0) {
                message.addDataElement("HOUR_TOTAL_RAINFALL", String.format("%.1f mm", totalRainfall));
                message.addDataElement("HOUR_AVERAGE_RAINFALL", String.format("%.2f mm", totalRainfall / validCount));
                message.addDataElement("HOUR_VALID_DATA_COUNT", String.valueOf(validCount));
            }
        }
        
        // 解析DRZ1时间序列数据
        String drzParsed = message.getDataElement("DRZ1_PARSED");
        if (drzParsed != null) {
            String[] values = drzParsed.split(",");
            double maxLevel = Double.MIN_VALUE;
            double minLevel = Double.MAX_VALUE;
            int validCount = 0;
            
            for (String value : values) {
                if (!value.contains("无效") && value.contains("m")) {
                    try {
                        double level = Double.parseDouble(value.replace("m", ""));
                        maxLevel = Math.max(maxLevel, level);
                        minLevel = Math.min(minLevel, level);
                        validCount++;
                    } catch (NumberFormatException ignored) {}
                }
            }
            
            if (validCount > 0) {
                message.addDataElement("HOUR_MAX_WATER_LEVEL", String.format("%.3f m", maxLevel));
                message.addDataElement("HOUR_MIN_WATER_LEVEL", String.format("%.3f m", minLevel));
                message.addDataElement("HOUR_WATER_LEVEL_RANGE", String.format("%.3f m", maxLevel - minLevel));
            }
        }
    }
    
    /**
     * 解析图像数据
     */
    public static void parsePictureData(SL651Message message) {
        String rawData = message.getRawData();
        if (rawData != null) {
            // 简单的图像数据分析
            message.addDataElement("PICTURE_DATA_LENGTH", String.valueOf(rawData.length()));
            message.addDataElement("PICTURE_FORMAT", "待解析");
            message.addDataElement("PICTURE_ENCODING", "十六进制");
        }
    }
    
    /**
     * 解析设备状态
     */
    public static void parseDeviceStatus(SL651Message message) {
        // 设备状态数据通常以特定格式编码
        String rawData = message.getRawData();
        if (rawData != null) {
            // 假设设备状态以特定位编码
            message.addDataElement("DEVICE_STATUS_RAW", rawData);
            message.addDataElement("DEVICE_TYPE", "水泵电机");
            // 这里需要根据具体协议规范解析设备状态位
        }
    }
    
    /**
     * 解析闸门状态
     */
    public static void parseGateStatus(SL651Message message) {
        String rawData = message.getRawData();
        if (rawData != null) {
            message.addDataElement("GATE_STATUS_RAW", rawData);
            message.addDataElement("GATE_TYPE", "水闸");
            // 这里需要根据具体协议规范解析闸门开度等信息
        }
    }
    
    /**
     * 计算统计信息
     */
    public static void calculateStatistics(SL651Message message) {
        // 在定时报文中添加数据统计分析
        message.addDataElement("REPORT_INTERVAL", "定时间隔报告");
        message.addDataElement("DATA_QUALITY", "良好"); // 可以根据实际情况评估
    }
    
    /**
     * 分析配置数据
     */
    public static void analyzeConfigurationData(SL651Message message) {
        // 人工置数报文可能包含遥测站参数设置
        String stationType = message.getDataElement(SL651Protocol.ELEM_STATION_TYPE);
        if (stationType != null) {
            message.addDataElement("CONFIG_TYPE", "遥测站类型设置");
            
            // 根据站点类型解析
            String stationTypeDesc = getStationTypeDescription(stationType);
            message.addDataElement("STATION_TYPE_DESC", stationTypeDesc);
        }
        
        // 检查是否包含时间配置
        String observeTime = message.getDataElement(SL651Protocol.ELEM_OBSERVE_TIME);
        if (observeTime != null) {
            message.addDataElement("CONFIG_TIME", "观测时间设置");
        }
        
        message.addDataElement("CONFIG_STATUS", "配置数据已记录");
    }
    
    /**
     * 解析气象数据
     */
    public static void parseWeatherData(SL651Message message) {
        // 解析温度数据
        String temperature = message.getDataElement("T");
        if (temperature != null) {
            try {
                double value = Double.parseDouble(temperature);
                message.addDataElement("TEMPERATURE_FORMATTED", String.format("%.1f℃", value));
                
                // 温度等级判断
                String level = getTemperatureLevel(value);
                message.addDataElement("TEMPERATURE_LEVEL", level);
            } catch (NumberFormatException e) {
                message.addDataElement("TEMPERATURE_FORMAT_ERROR", "温度数据格式错误");
            }
        }
        
        // 解析水温数据
        String waterTemp = message.getDataElement("TW");
        if (waterTemp != null) {
            try {
                double value = Double.parseDouble(waterTemp);
                message.addDataElement("WATER_TEMP_FORMATTED", String.format("%.1f℃", value));
            } catch (NumberFormatException e) {
                message.addDataElement("WATER_TEMP_FORMAT_ERROR", "水温数据格式错误");
            }
        }
    }
    
    /**
     * 解析视频数据
     */
    public static void parseVideoData(SL651Message message) {
        // 解析视频元数据
        message.addDataElement("VIDEO_STATUS", "视频数据已接收");
        
        // 视频时长
        String duration = message.getDataElement("DUR");
        if (duration != null) {
            message.addDataElement("VIDEO_DURATION", duration + " seconds");
        }
        
        // 视频格式
        String format = message.getDataElement("FMT");
        if (format != null) {
            message.addDataElement("VIDEO_FORMAT", format);
        }
    }
    
    // 专业解析方法实现
    private static void parseTemperatureData(String temp, SL651Message message) {
        try {
            double value = Double.parseDouble(temp);
            message.addDataElement("TEMP_CELSIUS", String.format("%.1f℃", value));
            message.addDataElement("TEMP_FAHRENHEIT", String.format("%.1f℉", value * 9/5 + 32));
            
            // 温度等级判定
            if (value < -10) message.addDataElement("TEMP_LEVEL", "严寒");
            else if (value < 0) message.addDataElement("TEMP_LEVEL", "寒冷");
            else if (value < 15) message.addDataElement("TEMP_LEVEL", "低温");
            else if (value < 25) message.addDataElement("TEMP_LEVEL", "适宜");
            else if (value < 35) message.addDataElement("TEMP_LEVEL", "炎热");
            else message.addDataElement("TEMP_LEVEL", "酷热");
            
        } catch (Exception e) {
            message.addDataElement("TEMP_PARSE_ERROR", "温度解析异常: " + e.getMessage());
        }
    }
    
    private static void parseWindData(String direction, String speed, SL651Message message) {
        try {
            int dir = Integer.parseInt(direction);
            double spd = Double.parseDouble(speed);
            
            // 风向解析
            String windDir = getWindDirectionName(dir);
            message.addDataElement("WIND_DIRECTION_NAME", windDir);
            
            // 风力等级
            int windLevel = getWindLevel(spd);
            message.addDataElement("WIND_LEVEL", windLevel + "级");
            message.addDataElement("WIND_DESCRIPTION", getWindDescription(windLevel));
            
        } catch (Exception e) {
            message.addDataElement("WIND_PARSE_ERROR", "风力解析异常: " + e.getMessage());
        }
    }
    
    private static void parsePressureData(String pressure, SL651Message message) {
        try {
            double value = Double.parseDouble(pressure);
            message.addDataElement("PRESSURE_HPA", String.format("%.1f hPa", value));
            message.addDataElement("PRESSURE_MMHG", String.format("%.1f mmHg", value * 0.750062));
            
            // 气压等级判定
            if (value < 1000) message.addDataElement("PRESSURE_LEVEL", "低压");
            else if (value < 1020) message.addDataElement("PRESSURE_LEVEL", "正常");
            else message.addDataElement("PRESSURE_LEVEL", "高压");
            
        } catch (Exception e) {
            message.addDataElement("PRESSURE_PARSE_ERROR", "气压解析异常: " + e.getMessage());
        }
    }
    
    private static void parseVisibilityData(String visibility, SL651Message message) {
        try {
            double value = Double.parseDouble(visibility);
            message.addDataElement("VISIBILITY_KM", String.format("%.1f km", value));
            
            // 能见度等级
            if (value < 1) message.addDataElement("VISIBILITY_LEVEL", "浓雾");
            else if (value < 5) message.addDataElement("VISIBILITY_LEVEL", "雾");
            else if (value < 10) message.addDataElement("VISIBILITY_LEVEL", "轻雾");
            else message.addDataElement("VISIBILITY_LEVEL", "良好");
            
        } catch (Exception e) {
            message.addDataElement("VISIBILITY_PARSE_ERROR", "能见度解析异常: " + e.getMessage());
        }
    }
    
    private static void parseVoltageStatus(String voltage, SL651Message message) {
        try {
            double value = Double.parseDouble(voltage);
            
            // 电压状态判定
            if (value < 10) {
                message.addDataElement("VOLTAGE_STATUS", "电压过低");
                message.addDataElement("DEVICE_ALERT", "设备可能断电");
            } else if (value < 11.5) {
                message.addDataElement("VOLTAGE_STATUS", "电压偏低");
                message.addDataElement("DEVICE_ALERT", "需要检查电源");
            } else if (value < 13.5) {
                message.addDataElement("VOLTAGE_STATUS", "电压正常");
            } else {
                message.addDataElement("VOLTAGE_STATUS", "电压偏高");
                message.addDataElement("DEVICE_ALERT", "需要检查充电系统");
            }
            
        } catch (Exception e) {
            message.addDataElement("VOLTAGE_PARSE_ERROR", "电压解析异常: " + e.getMessage());
        }
    }
    
    private static void analyzeCommunicationStatus(SL651Message message) {
        // 基于报文质量分析通信状态
        boolean crcValid = message.isCrcValid();
        String functionCode = message.getFunctionCode();
        
        if (crcValid) {
            message.addDataElement("COMM_STATUS", "通信正常");
            message.addDataElement("COMM_QUALITY", "良好");
        } else {
            message.addDataElement("COMM_STATUS", "通信异常");
            message.addDataElement("COMM_QUALITY", "差");
        }
        
        // 根据功能码判断通信类型
        if ("2F".equals(functionCode)) {
            message.addDataElement("COMM_TYPE", "心跳通信");
        } else if ("36".equals(functionCode)) {
            message.addDataElement("COMM_TYPE", "实时数据通信");
        } else {
            message.addDataElement("COMM_TYPE", "常规数据通信");
        }
    }
    
    private static void analyzeDeviceOperationStatus(SL651Message message) {
        // 综合分析设备运行状态
        String voltage = message.getDataElement(SL651Protocol.ELEM_VOLTAGE);
        String temperature = message.getDataElement(SL651Protocol.ELEM_TEMPERATURE);
        
        boolean deviceOk = true;
        StringBuilder issues = new StringBuilder();
        
        if (voltage != null) {
            try {
                double v = Double.parseDouble(voltage);
                if (v < 11.0) {
                    deviceOk = false;
                    issues.append("电压过低;");
                }
            } catch (Exception e) {}
        }
        
        if (temperature != null) {
            try {
                double t = Double.parseDouble(temperature);
                if (t < -20 || t > 50) {
                    deviceOk = false;
                    issues.append("温度异常;");
                }
            } catch (Exception e) {}
        }
        
        message.addDataElement("DEVICE_STATUS", deviceOk ? "正常运行" : "需要维护");
        if (!deviceOk) {
            message.addDataElement("DEVICE_ISSUES", issues.toString());
        }
    }
    
    private static void parseRainfallTimeSeries(String drpData, SL651Message message) {
        try {
            if (drpData.length() >= 24) {
                double[] rainfall = new double[12];
                double totalRainfall = 0;
                int validCount = 0;
                
                for (int i = 0; i < 12; i++) {
                    String hexValue = drpData.substring(i * 2, i * 2 + 2);
                    if (!"FF".equals(hexValue)) {
                        rainfall[i] = Integer.parseInt(hexValue, 16) * 0.1;
                        totalRainfall += rainfall[i];
                        validCount++;
                    }
                }
                
                message.addDataElement("RAINFALL_TOTAL_HOUR", String.format("%.1fmm", totalRainfall));
                message.addDataElement("RAINFALL_VALID_POINTS", String.valueOf(validCount));
                message.addDataElement("RAINFALL_DATA_RATE", String.format("%.1f%%", validCount * 100.0 / 12));
                
                // 降雨强度分析
                if (totalRainfall == 0) {
                    message.addDataElement("RAINFALL_INTENSITY", "无降雨");
                } else if (totalRainfall < 2.5) {
                    message.addDataElement("RAINFALL_INTENSITY", "小雨");
                } else if (totalRainfall < 8) {
                    message.addDataElement("RAINFALL_INTENSITY", "中雨");
                } else if (totalRainfall < 15) {
                    message.addDataElement("RAINFALL_INTENSITY", "大雨");
                } else {
                    message.addDataElement("RAINFALL_INTENSITY", "暴雨");
                }
            }
        } catch (Exception e) {
            message.addDataElement("RAINFALL_SERIES_ERROR", "时间序列解析异常: " + e.getMessage());
        }
    }
    
    private static void parseObservationTimeSequence(String observeTime, SL651Message message) {
        try {
            if (observeTime.length() == 12) {
                String year = "20" + observeTime.substring(0, 2);
                String month = observeTime.substring(2, 4);
                String day = observeTime.substring(4, 6);
                String hour = observeTime.substring(6, 8);
                String minute = observeTime.substring(8, 10);
                String second = observeTime.substring(10, 12);
                
                String formattedTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
                message.addDataElement("OBSERVE_TIME_FORMATTED", formattedTime);
                
                // 时间质量分析
                int h = Integer.parseInt(hour);
                int m = Integer.parseInt(minute);
                
                if (m == 0 && Integer.parseInt(second) == 0) {
                    message.addDataElement("TIME_TYPE", "整点观测");
                } else if (m % 5 == 0) {
                    message.addDataElement("TIME_TYPE", "5分钟观测");
                } else {
                    message.addDataElement("TIME_TYPE", "非标准时间观测");
                }
            }
        } catch (Exception e) {
            message.addDataElement("OBSERVE_TIME_ERROR", "观测时间解析异常: " + e.getMessage());
        }
    }
    
    /**
     * 获取降水等级
     */
    public static String getRainfallLevel(double rainfall) {
        if (rainfall == 0) return "无雨";
        else if (rainfall < 0.1) return "微量";
        else if (rainfall < 10) return "小雨";
        else if (rainfall < 25) return "中雨";
        else if (rainfall < 50) return "大雨";
        else if (rainfall < 100) return "暴雨";
        else if (rainfall < 250) return "大暴雨";
        else return "特大暴雨";
    }
    
    /**
     * 获取水位警戒等级
     */
    public static String getWaterLevelAlert(double waterLevel) {
        // 这些阈值需要根据实际站点配置
        if (waterLevel < 5.0) return "正常";
        else if (waterLevel < 8.0) return "注意";
        else if (waterLevel < 10.0) return "警戒";
        else if (waterLevel < 12.0) return "危险";
        else return "紧急";
    }
    
    /**
     * 获取电压状态
     */
    public static String getVoltageStatus(double voltage) {
        if (voltage < 10.0) return "电压过低";
        else if (voltage > 15.0) return "电压过高";
        else if (voltage >= 12.0 && voltage <= 13.0) return "电压正常";
        else return "电压偏离";
    }
    
    /**
     * 获取流量等级
     */
    public static String getFlowRateLevel(double flowRate) {
        if (flowRate == 0) return "无流量";
        else if (flowRate < 1.0) return "小流量";
        else if (flowRate < 10.0) return "中等流量";
        else if (flowRate < 50.0) return "大流量";
        else return "特大流量";
    }
    
    /**
     * 获取水温状态
     */
    public static String getWaterTempStatus(double temperature) {
        if (temperature < 0) return "冰点以下";
        else if (temperature < 10) return "低温";
        else if (temperature < 25) return "正常";
        else if (temperature < 35) return "偏高";
        else return "高温";
    }
    
    /**
     * 获取PH值等级
     */
    public static String getPhLevel(double pH) {
        if (pH < 4.0) return "强酸性";
        else if (pH < 6.5) return "酸性";
        else if (pH <= 8.5) return "中性";
        else if (pH <= 10.0) return "碱性";
        else return "强碱性";
    }
    
    /**
     * 获取溶解氧水质评级
     */
    public static String getDissolvedOxygenQuality(double dissolvedOxygen) {
        if (dissolvedOxygen >= 7.5) return "优秀";
        else if (dissolvedOxygen >= 6.0) return "良好";
        else if (dissolvedOxygen >= 5.0) return "一般";
        else if (dissolvedOxygen >= 3.0) return "较差";
        else return "很差";
    }
    
    /**
     * 获取电导率水质分级
     */
    public static String getConductivityGrade(double conductivity) {
        if (conductivity <= 300) return "优质";
        else if (conductivity <= 500) return "良好";
        else if (conductivity <= 800) return "一般";
        else if (conductivity <= 1200) return "较差";
        else return "很差";
    }
    
    /**
     * 获取浊度水质等级
     */
    public static String getTurbidityQuality(double turbidity) {
        if (turbidity <= 1.0) return "优秀";
        else if (turbidity <= 3.0) return "良好";
        else if (turbidity <= 6.0) return "一般";
        else if (turbidity <= 10.0) return "较差";
        else return "很差";
    }
    
    /**
     * 获取站点类型描述
     */
    public static String getStationTypeDescription(String stationType) {
        // 根据SL651标准的站点分类码定义
        if (stationType == null || stationType.trim().isEmpty()) {
            return "未定义";
        }
        
        switch (stationType.trim()) {
            case "11": return "雨量站";
            case "12": return "水位站";
            case "13": return "水文站";
            case "14": return "水质站";
            case "15": return "地下水站";
            case "16": return "土壤水分站";
            case "17": return "蒸发站";
            case "21": return "闸坝站";
            case "22": return "泵站";
            case "23": return "水库站";
            case "24": return "河口站";
            case "25": return "闸门站";
            default: return "其他类型(" + stationType + ")";
        }
    }
    
    /**
     * 获取温度等级
     */
    public static String getTemperatureLevel(double temperature) {
        if (temperature < 0) {
            return "冰冻";
        } else if (temperature < 10) {
            return "低温";
        } else if (temperature < 25) {
            return "适宜";
        } else if (temperature < 35) {
            return "热";
        } else {
            return "高温";
        }
    }
    
    // 辅助方法
    private static String getWindDirectionName(int direction) {
        String[] directions = {"北", "北东北", "东北", "东东北", "东", "东东南", 
                              "东南", "南东南", "南", "南西南", "西南", "西西南", 
                              "西", "西西北", "西北", "北西北"};
        int index = (int) Math.round(direction / 22.5) % 16;
        return directions[index];
    }
    
    private static int getWindLevel(double windSpeed) {
        if (windSpeed < 0.3) return 0;
        else if (windSpeed < 1.6) return 1;
        else if (windSpeed < 3.4) return 2;
        else if (windSpeed < 5.5) return 3;
        else if (windSpeed < 8.0) return 4;
        else if (windSpeed < 10.8) return 5;
        else if (windSpeed < 13.9) return 6;
        else if (windSpeed < 17.2) return 7;
        else if (windSpeed < 20.8) return 8;
        else if (windSpeed < 24.5) return 9;
        else if (windSpeed < 28.5) return 10;
        else if (windSpeed < 32.7) return 11;
        else return 12;
    }
    
    private static String getWindDescription(int level) {
        String[] descriptions = {"无风", "软风", "轻风", "微风", "和风", "清风", 
                               "强风", "疾风", "大风", "烈风", "狂风", "暴风", "飓风"};
        return level < descriptions.length ? descriptions[level] : "超强飓风";
    }
}